﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 火灾外勤
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireFieldController : ControllerBase
    {
        /// <summary>
        ///  增加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(FireFieldRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            string ip = HttpContext.GetClientIp();
            Firefield firefield = new Firefield();
            firefield.FireId = obj.FireId;
            firefield.SendMode = obj.SendMode;
            firefield.SceneUserId = obj.SceneUserId;
            firefield.UserId = obj.UserId;
            firefield.TransDatetime = obj.TransDatetime.HasValue ? obj.TransDatetime.Value : DateTime.Now;
            firefield.FieldContent = obj.FieldContent;
            firefield.EstimateLatitude = obj.EstimateLatitude;
            firefield.EstimateLongitude = obj.EstimateLongitude;
            firefield.EstimateAltitude = obj.EstimateAltitude;
            firefield.Ip = ip;

            Firesite firesite = new Firesite();
            firesite.FireId = obj.FireId;
            firesite.Latitude = obj.EstimateLatitude;
            firesite.Longitude = obj.EstimateLongitude;
            firesite.Altitude = obj.EstimateAltitude;
            firesite.UserId = obj.UserId;
            firesite.TransDatetime = firefield.TransDatetime;
            firesite.Ip = firefield.Ip;
            try
            {
                var data = BLL.FireFieldBLL.Instance.Add(firefield, firesite);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status = data.Status;
                result.Message = data.Message;

            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireField/Add Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }



        /// <summary>
        ///  完成 到达现场任务
        /// </summary>
        /// <param name="obj">obj.FireId</param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Complete(FireCompleteFieldRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                if (obj.FireId == 0 || obj.SceneUserId == 0)
                {
                    result.Status = 0;
                    result.Message = "FireId Or SceneUserId Equals 0";
                }
                else 
                {
                    string ip = HttpContext.GetClientIp();
                    List<Firefieldattach> firefieldattachs = new List<Firefieldattach>();
                    if (obj.Attachs != null)
                    {
                        foreach (var item in obj.Attachs)
                        {
                            Firefieldattach firefieldattach = new Firefieldattach();
                            firefieldattach.Direction = item.Direction;
                            firefieldattach.ShotLatitude = item.ShotLatitude;
                            firefieldattach.ShotLongitude = item.ShotLongitude;
                            firefieldattach.ShotAltitude = item.ShotAltitude;
                            firefieldattach.SceneFile = item.SceneFile;
                            firefieldattachs.Add(firefieldattach);
                        }
                    }
                    var data = BLL.FireFieldBLL.Instance.Complete(obj.FireId, obj.SceneUserId, obj.SceneContent, firefieldattachs, obj.SceneTime,obj.ReviseLatitude,obj.ReviseLongitude, obj.ReviseAltitude, ip);
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                    result.Status = data.Status;
                    result.Message = data.Message;
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireField/Complete Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }
    }
}